

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="Description" content="scikit-learn: machine learning in Python">

  
  <title>Developers’ Tips and Tricks &mdash; scikit-learn 0.22 documentation</title>
  
  <link rel="canonical" href="http://scikit-learn.org/stable/developers/tips.html" />

  
  <link rel="shortcut icon" href="../_static/favicon.ico"/>
  

  <link rel="stylesheet" href="../_static/css/vendor/bootstrap.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/gallery.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script> 
</head>
<body>
<nav id="navbar" class="sk-docs-navbar navbar navbar-expand-md navbar-light bg-light py-0">
  <div class="container-fluid sk-docs-container px-0">
      <a class="navbar-brand py-0" href="../index.html">
        <img
          class="sk-brand-img"
          src="../_static/scikit-learn-logo-small.png"
          alt="logo"/>
      </a>
    <button
      id="sk-navbar-toggler"
      class="navbar-toggler"
      type="button"
      data-toggle="collapse"
      data-target="#navbarSupportedContent"
      aria-controls="navbarSupportedContent"
      aria-expanded="false"
      aria-label="Toggle navigation"
    >
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="sk-navbar-collapse collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../install.html">Install</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../user_guide.html">User Guide</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../modules/classes.html">API</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../auto_examples/index.html">Examples</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../getting_started.html">Getting Started</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../tutorial/index.html">Tutorial</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../glossary.html">Glossary</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="index.html">Development</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../faq.html">FAQ</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../related_projects.html">Related packages</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../roadmap.html">Roadmap</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../about.html">About us</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://github.com/scikit-learn/scikit-learn">GitHub</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://scikit-learn.org/dev/versions.html">Other Versions</a>
        </li>
        <li class="nav-item dropdown nav-more-item-dropdown">
          <a class="sk-nav-link nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">More</a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdown">
              <a class="sk-nav-dropdown-item dropdown-item" href="../getting_started.html">Getting Started</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../tutorial/index.html">Tutorial</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../glossary.html">Glossary</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="index.html">Development</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../faq.html">FAQ</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../related_projects.html">Related packages</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../roadmap.html">Roadmap</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../about.html">About us</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="https://github.com/scikit-learn/scikit-learn">GitHub</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="https://scikit-learn.org/dev/versions.html">Other Versions</a>
          </div>
        </li>
      </ul>
      <div id="searchbox" role="search">
          <div class="searchformwrapper">
          <form class="search" action="../search.html" method="get">
            <input class="sk-search-text-input" type="text" name="q" aria-labelledby="searchlabel" />
            <input class="sk-search-text-btn" type="submit" value="Go" />
          </form>
          </div>
      </div>
    </div>
  </div>
</nav>
<div class="d-flex" id="sk-doc-wrapper">
    <input type="checkbox" name="sk-toggle-checkbox" id="sk-toggle-checkbox">
    <label id="sk-sidemenu-toggle" class="sk-btn-toggle-toc btn sk-btn-primary" for="sk-toggle-checkbox">Toggle Menu</label>
    <div id="sk-sidebar-wrapper" class="border-right">
      <div class="sk-sidebar-toc-wrapper">
        <div class="sk-sidebar-toc-logo">
          <a href="../index.html">
            <img
              class="sk-brand-img"
              src="../_static/scikit-learn-logo-small.png"
              alt="logo"/>
          </a>
        </div>
        <div class="btn-group w-100 mb-2" role="group" aria-label="rellinks">
            <a href="develop.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Developing scikit-learn estimators">Prev</a><a href="index.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Developer’s Guide">Up</a>
            <a href="utilities.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Utilities for Developers">Next</a>
        </div>
        <div class="alert alert-danger p-1 mb-2" role="alert">
          <p class="text-center mb-0">
          <strong>scikit-learn 0.22</strong><br/>
          <a href="http://scikit-learn.org/dev/versions.html">Other versions</a>
          </p>
        </div>
        <div class="alert alert-warning p-1 mb-2" role="alert">
          <p class="text-center mb-0">
            Please <a class="font-weight-bold" href="../about.html#citing-scikit-learn"><string>cite us</string></a> if you use the software.
          </p>
        </div>
          <div class="sk-sidebar-toc">
            <ul>
<li><a class="reference internal" href="#">Developers’ Tips and Tricks</a><ul>
<li><a class="reference internal" href="#productivity-and-sanity-preserving-tips">Productivity and sanity-preserving tips</a><ul>
<li><a class="reference internal" href="#viewing-the-rendered-html-documentation-for-a-pull-request">Viewing the rendered HTML documentation for a pull request</a></li>
<li><a class="reference internal" href="#folding-and-unfolding-outdated-diffs-on-pull-requests">Folding and unfolding outdated diffs on pull requests</a></li>
<li><a class="reference internal" href="#checking-out-pull-requests-as-remote-tracking-branches">Checking out pull requests as remote-tracking branches</a></li>
<li><a class="reference internal" href="#display-code-coverage-in-pull-requests">Display code coverage in pull requests</a></li>
<li><a class="reference internal" href="#useful-pytest-aliases-and-flags">Useful pytest aliases and flags</a></li>
<li><a class="reference internal" href="#standard-replies-for-reviewing">Standard replies for reviewing</a></li>
</ul>
</li>
<li><a class="reference internal" href="#debugging-memory-errors-in-cython-with-valgrind">Debugging memory errors in Cython with valgrind</a></li>
</ul>
</li>
</ul>

          </div>
      </div>
    </div>
    <div id="sk-page-content-wrapper">
      <div class="sk-page-content container-fluid body px-md-3" role="main">
        
  <div class="section" id="developers-tips-and-tricks">
<span id="developers-tips"></span><h1>Developers’ Tips and Tricks<a class="headerlink" href="#developers-tips-and-tricks" title="Permalink to this headline">¶</a></h1>
<div class="section" id="productivity-and-sanity-preserving-tips">
<h2>Productivity and sanity-preserving tips<a class="headerlink" href="#productivity-and-sanity-preserving-tips" title="Permalink to this headline">¶</a></h2>
<p>In this section we gather some useful advice and tools that may increase your
quality-of-life when reviewing pull requests, running unit tests, and so forth.
Some of these tricks consist of userscripts that require a browser extension
such as <a class="reference external" href="https://tampermonkey.net/">TamperMonkey</a> or <a class="reference external" href="https://www.greasespot.net/">GreaseMonkey</a>; to set up userscripts you must have
one of these extensions installed, enabled and running.  We provide userscripts
as GitHub gists; to install them, click on the “Raw” button on the gist page.</p>
<div class="section" id="viewing-the-rendered-html-documentation-for-a-pull-request">
<span id="viewing-rendered-html-documentation"></span><h3>Viewing the rendered HTML documentation for a pull request<a class="headerlink" href="#viewing-the-rendered-html-documentation-for-a-pull-request" title="Permalink to this headline">¶</a></h3>
<p>We use CircleCI to build the HTML documentation for every pull request. To
access that documentation, instructions are provided in the <a class="reference internal" href="contributing.html#contribute-documentation"><span class="std std-ref">documentation
section of the contributor guide</span></a>. To save you a few
clicks, we provide a <a class="reference external" href="https://raw.githubusercontent.com/lesteve/userscripts/master/add-button-for-pr-circleci-doc.user.js">userscript</a>
that adds a button to every PR. After installing the userscript, navigate to
any GitHub PR; a new button labeled “See CircleCI doc for this PR” should
appear in the top-right area.</p>
</div>
<div class="section" id="folding-and-unfolding-outdated-diffs-on-pull-requests">
<h3>Folding and unfolding outdated diffs on pull requests<a class="headerlink" href="#folding-and-unfolding-outdated-diffs-on-pull-requests" title="Permalink to this headline">¶</a></h3>
<p>GitHub hides discussions on PRs when the corresponding lines of code have been
changed in the mean while. This <a class="reference external" href="https://raw.githubusercontent.com/lesteve/userscripts/master/github-expand-all.user.js">userscript</a>
provides a shortcut (Control-Alt-P at the time of writing but look at the code
to be sure) to unfold all such hidden discussions at once, so you can catch up.</p>
</div>
<div class="section" id="checking-out-pull-requests-as-remote-tracking-branches">
<h3>Checking out pull requests as remote-tracking branches<a class="headerlink" href="#checking-out-pull-requests-as-remote-tracking-branches" title="Permalink to this headline">¶</a></h3>
<p>In your local fork, add to your <code class="docutils literal notranslate"><span class="pre">.git/config</span></code>, under the <code class="docutils literal notranslate"><span class="pre">[remote</span>
<span class="pre">&quot;upstream&quot;]</span></code> heading, the line:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">fetch</span> <span class="o">=</span> <span class="o">+</span><span class="n">refs</span><span class="o">/</span><span class="n">pull</span><span class="o">/*/</span><span class="n">head</span><span class="p">:</span><span class="n">refs</span><span class="o">/</span><span class="n">remotes</span><span class="o">/</span><span class="n">upstream</span><span class="o">/</span><span class="n">pr</span><span class="o">/*</span>
</pre></div>
</div>
<p>You may then use <code class="docutils literal notranslate"><span class="pre">git</span> <span class="pre">checkout</span> <span class="pre">pr/PR_NUMBER</span></code> to navigate to the code of the
pull-request with the given number. (<a class="reference external" href="https://gist.github.com/piscisaureus/3342247">Read more in this gist.</a>)</p>
</div>
<div class="section" id="display-code-coverage-in-pull-requests">
<h3>Display code coverage in pull requests<a class="headerlink" href="#display-code-coverage-in-pull-requests" title="Permalink to this headline">¶</a></h3>
<p>To overlay the code coverage reports generated by the CodeCov continuous
integration, consider <a class="reference external" href="https://github.com/codecov/browser-extension">this browser extension</a>. The coverage of each line
will be displayed as a color background behind the line number.</p>
</div>
<div class="section" id="useful-pytest-aliases-and-flags">
<span id="pytest-tips"></span><h3>Useful pytest aliases and flags<a class="headerlink" href="#useful-pytest-aliases-and-flags" title="Permalink to this headline">¶</a></h3>
<p>The full test suite takes fairly long to run. For faster iterations,
it is possibly to select a subset of tests using pytest selectors.
In particular, one can run a <a class="reference external" href="https://docs.pytest.org/en/latest/example/markers.html#selecting-tests-based-on-their-node-id">single test based on its node ID</a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pytest</span> <span class="o">-</span><span class="n">v</span> <span class="n">sklearn</span><span class="o">/</span><span class="n">linear_model</span><span class="o">/</span><span class="n">tests</span><span class="o">/</span><span class="n">test_logistic</span><span class="o">.</span><span class="n">py</span><span class="p">::</span><span class="n">test_sparsify</span>
</pre></div>
</div>
<p>or use the <a class="reference external" href="https://docs.pytest.org/en/latest/example/markers.html#using-k-expr-to-select-tests-based-on-their-name">-k pytest parameter</a>
to select tests based on their name. For instance,:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pytest</span> <span class="n">sklearn</span><span class="o">/</span><span class="n">tests</span><span class="o">/</span><span class="n">test_common</span><span class="o">.</span><span class="n">py</span> <span class="o">-</span><span class="n">v</span> <span class="o">-</span><span class="n">k</span> <span class="n">LogisticRegression</span>
</pre></div>
</div>
<p>will run all <a class="reference internal" href="../glossary.html#term-common-tests"><span class="xref std std-term">common tests</span></a> for the <code class="docutils literal notranslate"><span class="pre">LogisticRegression</span></code> estimator.</p>
<p>When a unit test fails, the following tricks can make debugging easier:</p>
<blockquote>
<div><ol class="arabic">
<li><p>The command line argument <code class="docutils literal notranslate"><span class="pre">pytest</span> <span class="pre">-l</span></code> instructs pytest to print the local
variables when a failure occurs.</p></li>
<li><p>The argument <code class="docutils literal notranslate"><span class="pre">pytest</span> <span class="pre">--pdb</span></code> drops into the Python debugger on failure. To
instead drop into the rich IPython debugger <code class="docutils literal notranslate"><span class="pre">ipdb</span></code>, you may set up a
shell alias to:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pytest</span> <span class="o">--</span><span class="n">pdbcls</span><span class="o">=</span><span class="n">IPython</span><span class="o">.</span><span class="n">terminal</span><span class="o">.</span><span class="n">debugger</span><span class="p">:</span><span class="n">TerminalPdb</span> <span class="o">--</span><span class="n">capture</span> <span class="n">no</span>
</pre></div>
</div>
</li>
</ol>
</div></blockquote>
<p>Other <code class="docutils literal notranslate"><span class="pre">pytest</span></code> options that may become useful include:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">-x</span></code> which exits on the first failed test</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">--lf</span></code> to rerun the tests that failed on the previous run</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">--ff</span></code> to rerun all previous tests, running the ones that failed first</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-s</span></code> so that pytest does not capture the output of <code class="docutils literal notranslate"><span class="pre">print()</span></code>
statements</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">--tb=short</span></code> or <code class="docutils literal notranslate"><span class="pre">--tb=line</span></code> to control the length of the logs</p></li>
</ul>
</div></blockquote>
<p>Since our continuous integration tests will error if
<code class="docutils literal notranslate"><span class="pre">FutureWarning</span></code> isn’t properly caught,
it is also recommended to run <code class="docutils literal notranslate"><span class="pre">pytest</span></code> along with the
<code class="docutils literal notranslate"><span class="pre">-Werror::FutureWarning</span></code> flag.</p>
</div>
<div class="section" id="standard-replies-for-reviewing">
<span id="saved-replies"></span><h3>Standard replies for reviewing<a class="headerlink" href="#standard-replies-for-reviewing" title="Permalink to this headline">¶</a></h3>
<p>It may be helpful to store some of these in GitHub’s <a class="reference external" href="https://github.com/settings/replies/">saved
replies</a> for reviewing:</p>
<dl>
<dt>Issue: Usage questions</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>You&#39;re asking a usage question. The issue tracker is mainly for bugs and new features. For usage questions, it is recommended to try [Stack Overflow](https://stackoverflow.com/questions/tagged/scikit-learn) or [the Mailing List](https://mail.python.org/mailman/listinfo/scikit-learn).
</pre></div>
</div>
</dd>
<dt>Issue: You’re welcome to update the docs</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Please feel free to offer a pull request updating the documentation if you feel it could be improved.
</pre></div>
</div>
</dd>
<dt>Issue: Self-contained example for bug</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Please provide [self-contained example code](https://stackoverflow.com/help/mcve), including imports and data (if possible), so that other contributors can just run it and reproduce your issue. Ideally your example code should be minimal.
</pre></div>
</div>
</dd>
<dt>Issue: Software versions</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>To help diagnose your issue, please paste the output of:
```py
import sklearn; sklearn.show_versions()
```
Thanks.
</pre></div>
</div>
</dd>
<dt>Issue: Code blocks</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Readability can be greatly improved if you [format](https://help.github.com/articles/creating-and-highlighting-code-blocks/) your code snippets and complete error messages appropriately. For example:

    ```python
    print(something)
    ```
generates:
```python
print(something)
```
And:

    ```pytb
    Traceback (most recent call last):
      File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
    ImportError: No module named &#39;hello&#39;
    ```
generates:
```pytb
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
ImportError: No module named &#39;hello&#39;
```
You can edit your issue descriptions and comments at any time to improve readability. This helps maintainers a lot. Thanks!
</pre></div>
</div>
</dd>
<dt>Issue/Comment: Linking to code</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Friendly advice: for clarity&#39;s sake, you can link to code like [this](https://help.github.com/articles/creating-a-permanent-link-to-a-code-snippet/).
</pre></div>
</div>
</dd>
<dt>Issue/Comment: Linking to comments</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Please use links to comments, which make it a lot easier to see what you are referring to, rather than just linking to the issue. See [this](https://stackoverflow.com/questions/25163598/how-do-i-reference-a-specific-issue-comment-on-github) for more details.
</pre></div>
</div>
</dd>
<dt>PR-NEW: Better description</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Thanks for the pull request! Please make the title of the PR descriptive so that we can easily recall the issue it is resolving. You should state what issue (or PR) it fixes/resolves in the description (see [here](http://scikit-learn.org/dev/developers/contributing.html#contributing-pull-requests)).
</pre></div>
</div>
</dd>
<dt>PR-NEW: Fix #</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Please use &quot;Fix #issueNumber&quot; in your PR description (and you can do it more than once). This way the associated issue gets closed automatically when the PR is merged. For more details, look at [this](https://github.com/blog/1506-closing-issues-via-pull-requests).
</pre></div>
</div>
</dd>
<dt>PR-NEW or Issue: Maintenance cost</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Every feature we include has a [maintenance cost](http://scikit-learn.org/dev/faq.html#why-are-you-so-selective-on-what-algorithms-you-include-in-scikit-learn). Our maintainers are mostly volunteers. For a new feature to be included, we need evidence that it is often useful and, ideally, [well-established](http://scikit-learn.org/dev/faq.html#what-are-the-inclusion-criteria-for-new-algorithms) in the literature or in practice. That doesn&#39;t stop you implementing it for yourself and publishing it in a separate repository, or even [scikit-learn-contrib](https://scikit-learn-contrib.github.io).
</pre></div>
</div>
</dd>
<dt>PR-WIP: What’s needed before merge?</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Please clarify (perhaps as a TODO list in the PR description) what work you believe still needs to be done before it can be reviewed for merge. When it is ready, please prefix the PR title with `[MRG]`.
</pre></div>
</div>
</dd>
<dt>PR-WIP: Regression test needed</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Please add a [non-regression test](https://en.wikipedia.org/wiki/Non-regression_testing) that would fail at master but pass in this PR.
</pre></div>
</div>
</dd>
<dt>PR-WIP: PEP8</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>You have some [PEP8](https://www.python.org/dev/peps/pep-0008/) violations, whose details you can see in the Circle CI `lint` job. It might be worth configuring your code editor to check for such errors on the fly, so you can catch them before committing.
</pre></div>
</div>
</dd>
<dt>PR-MRG: Patience</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Before merging, we generally require two core developers to agree that your pull request is desirable and ready. [Please be patient](http://scikit-learn.org/dev/faq.html#why-is-my-pull-request-not-getting-any-attention), as we mostly rely on volunteered time from busy core developers. (You are also welcome to help us out with [reviewing other PRs](http://scikit-learn.org/dev/developers/contributing.html#code-review-guidelines).)
</pre></div>
</div>
</dd>
<dt>PR-MRG: Add to what’s new</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Please add an entry to the change log at `doc/whats_new/v*.rst`. Like the other entries there, please reference this pull request with `:pr:` and credit yourself (and other contributors if applicable) with `:user:`.
</pre></div>
</div>
</dd>
<dt>PR: Don’t change unrelated</dt><dd><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Please do not change unrelated lines. It makes your contribution harder to review and may introduce merge conflicts to other pull requests.
</pre></div>
</div>
</dd>
</dl>
</div>
</div>
<div class="section" id="debugging-memory-errors-in-cython-with-valgrind">
<h2>Debugging memory errors in Cython with valgrind<a class="headerlink" href="#debugging-memory-errors-in-cython-with-valgrind" title="Permalink to this headline">¶</a></h2>
<p>While python/numpy’s built-in memory management is relatively robust, it can
lead to performance penalties for some routines. For this reason, much of
the high-performance code in scikit-learn in written in cython. This
performance gain comes with a tradeoff, however: it is very easy for memory
bugs to crop up in cython code, especially in situations where that code
relies heavily on pointer arithmetic.</p>
<p>Memory errors can manifest themselves a number of ways. The easiest ones to
debug are often segmentation faults and related glibc errors. Uninitialized
variables can lead to unexpected behavior that is difficult to track down.
A very useful tool when debugging these sorts of errors is
<a class="reference external" href="http://valgrind.org">valgrind</a>.</p>
<p>Valgrind is a command-line tool that can trace memory errors in a variety of
code. Follow these steps:</p>
<blockquote>
<div><ol class="arabic">
<li><p>Install <a class="reference external" href="http://valgrind.org">valgrind</a> on your system.</p></li>
<li><p>Download the python valgrind suppression file: <a class="reference external" href="https://svn.python.org/projects/python/trunk/Misc/valgrind-python.supp">valgrind-python.supp</a>.</p></li>
<li><p>Follow the directions in the <a class="reference external" href="https://svn.python.org/projects/python/trunk/Misc/README.valgrind">README.valgrind</a> file to customize your
python suppressions. If you don’t, you will have spurious output coming
related to the python interpreter instead of your own code.</p></li>
<li><p>Run valgrind as follows:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$&gt; valgrind -v --suppressions=valgrind-python.supp python my_test_script.py
</pre></div>
</div>
</li>
</ol>
</div></blockquote>
<p>The result will be a list of all the memory-related errors, which reference
lines in the C-code generated by cython from your .pyx file. If you examine
the referenced lines in the .c file, you will see comments which indicate the
corresponding location in your .pyx source file. Hopefully the output will
give you clues as to the source of your memory error.</p>
<p>For more information on valgrind and the array of options it has, see the
tutorials and documentation on the <a class="reference external" href="http://valgrind.org">valgrind web site</a>.</p>
</div>
</div>


      </div>
    <div class="container">
      <footer class="sk-content-footer">
            &copy; 2007 - 2019, scikit-learn developers (BSD License).
          <a href="../_sources/developers/tips.rst.txt" rel="nofollow">Show this page source</a>
      </footer>
    </div>
  </div>
</div>
<script src="../_static/js/vendor/bootstrap.min.js"></script>

<script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-22606712-2', 'auto');
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>


<script>
$(document).ready(function() {
    /* Add a [>>>] button on the top-right corner of code samples to hide
     * the >>> and ... prompts and the output and thus make the code
     * copyable. */
    var div = $('.highlight-python .highlight,' +
                '.highlight-python3 .highlight,' +
                '.highlight-pycon .highlight,' +
		'.highlight-default .highlight')
    var pre = div.find('pre');

    // get the styles from the current theme
    pre.parent().parent().css('position', 'relative');
    var hide_text = 'Hide prompts and outputs';
    var show_text = 'Show prompts and outputs';

    // create and add the button to all the code blocks that contain >>>
    div.each(function(index) {
        var jthis = $(this);
        if (jthis.find('.gp').length > 0) {
            var button = $('<span class="copybutton">&gt;&gt;&gt;</span>');
            button.attr('title', hide_text);
            button.data('hidden', 'false');
            jthis.prepend(button);
        }
        // tracebacks (.gt) contain bare text elements that need to be
        // wrapped in a span to work with .nextUntil() (see later)
        jthis.find('pre:has(.gt)').contents().filter(function() {
            return ((this.nodeType == 3) && (this.data.trim().length > 0));
        }).wrap('<span>');
    });

    // define the behavior of the button when it's clicked
    $('.copybutton').click(function(e){
        e.preventDefault();
        var button = $(this);
        if (button.data('hidden') === 'false') {
            // hide the code output
            button.parent().find('.go, .gp, .gt').hide();
            button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
            button.css('text-decoration', 'line-through');
            button.attr('title', show_text);
            button.data('hidden', 'true');
        } else {
            // show the code output
            button.parent().find('.go, .gp, .gt').show();
            button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
            button.css('text-decoration', 'none');
            button.attr('title', hide_text);
            button.data('hidden', 'false');
        }
    });

	/*** Add permalink buttons next to glossary terms ***/
	$('dl.glossary > dt[id]').append(function() {
		return ('<a class="headerlink" href="#' +
			    this.getAttribute('id') +
			    '" title="Permalink to this term">¶</a>');
	});
  /*** Hide navbar when scrolling down ***/
  // Returns true when headerlink target matches hash in url
  (function() {
    hashTargetOnTop = function() {
        var hash = window.location.hash;
        if ( hash.length < 2 ) { return false; }

        var target = document.getElementById( hash.slice(1) );
        if ( target === null ) { return false; }

        var top = target.getBoundingClientRect().top;
        return (top < 2) && (top > -2);
    };

    // Hide navbar on load if hash target is on top
    var navBar = document.getElementById("navbar");
    var navBarToggler = document.getElementById("sk-navbar-toggler");
    var navBarHeightHidden = "-" + navBar.getBoundingClientRect().height + "px";
    var $window = $(window);

    hideNavBar = function() {
        navBar.style.top = navBarHeightHidden;
    };

    showNavBar = function() {
        navBar.style.top = "0";
    }

    if (hashTargetOnTop()) {
        hideNavBar()
    }

    var prevScrollpos = window.pageYOffset;
    hideOnScroll = function(lastScrollTop) {
        if (($window.width() < 768) && (navBarToggler.getAttribute("aria-expanded") === 'true')) {
            return;
        }
        if (lastScrollTop > 2 && (prevScrollpos <= lastScrollTop) || hashTargetOnTop()){
            hideNavBar()
        } else {
            showNavBar()
        }
        prevScrollpos = lastScrollTop;
    };

    /*** high preformance scroll event listener***/
    var raf = window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        window.oRequestAnimationFrame;
    var lastScrollTop = $window.scrollTop();

    if (raf) {
        loop();
    }

    function loop() {
        var scrollTop = $window.scrollTop();
        if (lastScrollTop === scrollTop) {
            raf(loop);
            return;
        } else {
            lastScrollTop = scrollTop;
            hideOnScroll(lastScrollTop);
            raf(loop);
        }
    }
  })();
});

</script>
    
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
    
</body>
</html>